﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using bts_webapp.Models;

namespace bts_webapp
{
    public partial class UxPort : System.Web.UI.UserControl
    {
        public string PortCode
        {
            get
            {
                return this.txtPortCode.Text;
            }
            set { this.txtPortCode.Text = value; }
        }

        public string PortNumber
        {
            get
            {
                return this.txtPortNumber.Text;
            }
            set { this.txtPortNumber.Text=value; }
        }

        public string CardId
        {
            get
            {
                return this.comboCard.SelectedItem.Value;
            }
            set { this.UpdateCard(value); }
        }

        public string Description
        {
            get
            {
                return this.txtDescription.Text;
            }
            set { this.txtDescription.Text=value; }
        }

        public bool Status
        {
            get
            {
                return this.chkStatus.Checked;
            }
            set { this.chkStatus.Checked = value; }
        }

        public bool InsertOrUpdate
        {
            set
            {
                this.btnUpdate.CommandName = value ? "PerformInsert" : "Update";
                this.btnUpdate.Text = value ? "Insert" : "Update";
            }

        }

        //public void EnableFieldCode(bool enabled)
        //{
        //    this.txtPortCode.Enabled = enabled;
        //}

        public void BindData(VIBA_PORT port)
        {
            this.PortCode = port.Port_Id.ToString();
            this.PortNumber =port.Port_Number.ToString();
            this.CardId = port.Card_Id.ToString();
            this.Description = port.Description;
            this.Status = port.Status == 0 ? false : true;
        }

        public VIBA_PORT GetData()
        {
            VIBA_PORT port = new VIBA_PORT
            {
                Port_Number = Convert.ToInt32(this.PortNumber),
                Card_Id = Convert.ToInt32(this.CardId),
                Description = this.Description,
                Status = GetStatus(this.Status)               
            };
            return port;
        }

        private int GetStatus(bool status)
        {
            return status ? 1 : 0;
        }

        public void SetErrorStatus(string error)
        {
            if (error.Trim() != string.Empty)
                this.litStatus.Text = "<font color='red'>" + error + "</font>";
            else
                this.litStatus.Text = string.Empty;
        }

        private void UpdateCard(string cardId)
        {
            var item = this.comboCard.Items.FirstOrDefault(x => x.Value.Trim() == cardId.Trim());
            if (item != null)
            {
                var index = this.comboCard.Items.IndexOf(item);
                this.comboCard.SelectedIndex = index;
            }
        }

        public void AddComboBoxItems()
        {
            using (var db = new BTSModelDataContext())
            {
                var list = db.VIBA_CARDs;
                this.comboCard.DataSource = list;
                this.comboCard.DataTextField = "Card_TenCard";
                this.comboCard.DataValueField = "Card_Id";
                this.comboCard.DataBind();
                this.UpdateCard(this.CardId);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        internal void BindData(VIBA_SUCO port)
        {
            throw new NotImplementedException();
        }
    }
}